﻿<?php

//////////////////////////////////////////////////////////////////////////
//
// WikiHiero2 - A PHP convert from text using "Manual for the encoding of
// hieroglyphic texts for computer input" syntax to HTML entities (table and
// images).
//
// Copyright (C) 2004-2010 Guillaume Blanchard (Aoineko)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
//////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
/**
 *
 */
class WH_WHXml extends WH_Plugin 
{
	private $depth = 0; /// Current node depth
	private $parent;
	const   tab = "  "; /// Code for tabulation (can be "\t")

	////////////////////////////////////////////////////////////////////////////////
	/**
	 * Constructor
	 */
	function __construct()
	{
		// Internal variable
		$this->parent = array();
	}

	public static function CanImport() { return true; }
	public static function CanExport() { return true; }
	
	public static function GetId() { return "HWXml"; }
	public static function GetName() { return "WikiHiero XML"; }
	public static function GetVersion() { return "0.5"; }
	public static function GetDesc($type = WH_Plugin::TypeDefault, $lang = "") { return "Export hieroglyphs text in WikiHiero XML format";	}
	public static function GetCredit($lang = "") { return "Guillaume Blanchard (2004-2010)"; }
	
	////////////////////////////////////////////////////////////////////////////////
	/**
	 * Export a node
	 * @param	node	The node to be exported
	 * @return			XML code of the current node
	 */
	protected function &ExportNode(&$node)
	{
		$xml = "";
		for($i=0; $i<$this->depth; $i++)
			$xml .= self::tab;

			$xml .= "<wh_".WH_Document::GetClassName($node->GetClass());

		$content = null;
		foreach($node->GetAttributes() as $key => $value)
		{
			if($key == WH_Attribute::Content)
				$content = $value;
			else
				$xml .= " $key=\"".trim($value)."\"";
		}

		if($content)
		{
			if(strlen($content) > 256)
			{
				$xml .= ">\n";
				for($i=0; $i<$this->depth+1; $i++)
					$xml .= "  ";
				$xml .= "$content\n";
				for($i=0; $i<$this->depth; $i++)
					$xml .= "  ";
				$xml .= "</wh_".WH_Document::GetClassName($node->GetClass()).">\n";
			}
			else
				$xml .= ">$content</wh_".WH_Document::GetClassName($node->GetClass()).">\n";
		}
		else if(count($node->GetChildren()) == 0)
			$xml .= "/>\n";
		else
		{
			$xml .= ">\n";
			$this->depth++;
			foreach($node->GetChildren() as $child)
				$xml .= $this->ExportNode($child);
			$this->depth--;
			for($i=0; $i<$this->depth; $i++)
				$xml .= "  ";
			$xml .= "</wh_".WH_Document::GetClassName($node->GetClass()).">\n";
		}

		return $xml;
	}

	////////////////////////////////////////////////////////////////////////////////
	/**
	 *
	 */
	public function &Export(&$document)
	{
		$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n";
		$xml .= "<!DOCTYPE wh_doc SYSTEM \"doc/whxml.dtd\">\n";

		$this->depth = 0;
		$xml .= $this->ExportNode($document->GetRoot());

		return $xml;
	}

	////////////////////////////////////////////////////////////////////////////////
	/**
	 *
	 */
	function OnTagStart($parser, $name, array $attribs)
	{
		$name = strtolower(substr($name, 3));
		if($name == "doc")
			return;
		$id = WH_Document::GetClassId($name);
		if($id != -1)
		{
			$parent = end($this->parent);
			$node = &$parent->AddChild($id);
			foreach($attribs as $key => $value)
			{
				$node->SetAttribute(strtolower($key), $value);
			}
			array_push($this->parent, $node);
		}
	}

	////////////////////////////////////////////////////////////////////////////////
	/**
	 *
	 */
	function OnTagEnd($parser, $name)
	{
		$name = strtolower(substr($name, 3));
		if($name == "doc")
			return;
		$id = WH_Document::GetClassId($name);
		if($id != -1)
			array_pop($this->parent);
	}

	////////////////////////////////////////////////////////////////////////////////
	/**
	 *
	 */
	public function &Import(&$text)
	{
		$this->document = new WH_Document();
		array_push($this->parent, $this->document->GetRoot());
		
		$parser = xml_parser_create("UTF-8");
		xml_set_element_handler($parser, array(&$this,"OnTagStart"), array(&$this,"OnTagEnd"));
		xml_parse($parser, $text, true);
		xml_parser_free($parser);

		return $this->document;
	}
}

?>